home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / mychar.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  6KB  |  276 lines

  1. /*----------------------------------------------------------------------*/
  2. /* This routine prints a char from one of my fonts, it has cacheing     */
  3. /* for the char data, which could be extended to bitmap cacheing     */
  4. /*----------------------------------------------------------------------*/
  5. #include "all.h"
  6. #include "core.h"
  7. #include "color.h"
  8. extern struct gmodel g;
  9. #include "mygraph.h"
  10. #define true (!false)
  11. #define false 0
  12. #ifdef BIGINDIAN
  13. #define BYTE0 1
  14. #define BYTE1 0
  15. #else
  16. #define BYTE0 0
  17. #define BYTE1 1
  18. #endif
  19.  
  20. int font_file_vector(int ff,char *filename);
  21. int draw_char_pcode(char *s);
  22. char *fontdir(char *s);
  23. int my_load_font(int ff);
  24. double frx(char **s);
  25. unsigned char my_name[80];
  26. char *my_code[80];
  27. int my_ref[80];
  28. char *my_buff;
  29. int my_font[80];
  30. int my_pnt[257];
  31. int my_curfont;
  32. extern double font_lwidth;
  33. int char_plen(char *s);
  34. int get_char_pcode(int ff, int cc, char **pp);
  35. my_char(int ff, int cc)
  36. {
  37.     char *pp;
  38.     get_char_pcode(ff,cc,&pp);
  39.     draw_char_pcode(pp);
  40. }
  41. get_char_pcode(int ff, int cc, char **pp)
  42. {
  43.     int i,plen,mi,minref;
  44.  
  45. /* y:    Is char in font cache, if so draw it, inc ref */
  46.     /*  should use *memchr(s,c,n)  */
  47.     for (i=1;i<80;i++) {
  48.         if (my_name[i]==cc) {
  49.             if (my_font[i]==ff) {
  50.                 my_ref[i]++;
  51.                 *pp = my_code[i];
  52.                 return;
  53.             }
  54.         }
  55.     }
  56.  
  57. /* x:    Is that font currently loaded, if so put in cache at least used, goto y */
  58.     minref = 30000;
  59.     mi = 0;
  60.     if (my_curfont!=ff) my_load_font(ff);
  61.     /* Find least used cache character */
  62.     for (i=1;i<80;i++)  {
  63.         if (my_ref[i]<minref) {minref=my_ref[i]; mi = i;}
  64.     }
  65.     if (mi==0) mi=1;
  66.     plen = char_plen(my_buff+my_pnt[cc]);
  67.     if (my_code[mi]==0) my_code[mi] = myallocz(plen+1);
  68.     else        {
  69.     /*    my_code[mi] = realloc(my_code[mi],plen+1); */
  70.         myfree(my_code[mi]);
  71.         my_code[mi] = myalloc((plen+1));
  72.     }
  73.     if (my_code[mi]==0) gprint("Memory allocation failure, in myfont.c \n");
  74.     memcpy(my_code[mi],my_buff+my_pnt[cc],plen+1);
  75.     *pp = my_code[mi];
  76.     my_name[mi] = cc;
  77.     my_ref[mi] = 1;
  78.     my_font[mi] = ff;
  79. }
  80. /* free up memory used by vector font */
  81. freeavec()
  82. {
  83.     if (my_buff == NULL) return;
  84.     myfree(my_buff);
  85.     my_buff = 0;
  86.     my_curfont = 0;
  87. }
  88. char *gledir(char *s);
  89. my_load_font(int ff)
  90. {
  91.     char vector_file[60];
  92.     int i;
  93.     FILE *fin;
  94.     font_file_vector(ff,vector_file);
  95.     fin = fopen(fontdir(vector_file),READ_BIN);
  96.     if (fin==0) {
  97.         gprint("(Warning) can't find vector file {%s} replacing it.\n",fontdir(vector_file));
  98.         font_replace_vector(ff);
  99.         font_file_vector(ff,vector_file);
  100.         fin = fopen(fontdir(vector_file),READ_BIN);
  101.         if (fin==NULL) gle_abort("Font vector texcmr.fve not found\n");
  102.     }
  103.     fread(my_pnt,sizeof(i),256,fin);
  104.     /* gprint("Size of rest of font %d \n",my_pnt[0]); */
  105.     if (my_buff==0) my_buff = myallocz(10 + my_pnt[0]);
  106.     else         {
  107.         myfree(my_buff);
  108.         my_buff = myallocz(10 + my_pnt[0]);
  109.     }
  110.     if (my_buff==0) gprint("Memory allocation failure MY_BUFF , in myfont.c \n");
  111.     fread(my_buff,1,my_pnt[0],fin);
  112.     fclose(fin);
  113.     my_curfont = ff;
  114. }
  115. int frxi(char **s);
  116. char_plen(char *s)
  117. {
  118.     char *savelen;
  119.     savelen = s;
  120.     while (*s!=15) {
  121.       switch (*s++) {
  122.         case 1:
  123.         case 2:
  124.         frxi(&s); frxi(&s);
  125.         break;
  126.         case 3:
  127.         frxi(&s); frxi(&s);
  128.         frxi(&s); frxi(&s);
  129.         frxi(&s); frxi(&s);
  130.         break;
  131.         case 4:
  132.         case 5:
  133.         case 6:
  134.         case 7:
  135.         case 8:
  136.         break;
  137.         case 0: /* char does not exist */
  138.         goto abort;
  139.         default:
  140.         gprint("Error in mychar pcode %d \n",*s++);
  141.         goto abort;
  142.       }
  143.  
  144.     }
  145. abort:
  146.     return s-savelen;
  147. }
  148. int draw_char_pcode(char *s)
  149. {
  150.     static double cx,cy,ox,oy,x1,y1,x2,y2;
  151.     char *savelen;
  152.     double old_lwidth;
  153.     int old_path,old_join;
  154.     int32 old_color;
  155.  
  156.     g_get_path(&old_path);
  157.     g_get_color(&old_color);
  158.     g_set_fill(old_color);
  159.     g_get_line_width(&old_lwidth);
  160.     g_set_line_width(font_lwidth);
  161.     g_get_line_join(&old_join);
  162.     g_set_line_join(1);    /* use rounded lines to avoid ucky peeks */
  163.  
  164.     g_get_xy(&ox,&oy);
  165.     savelen = s;
  166.     if (!old_path) {
  167.         g_set_path(true);
  168.         g_newpath();
  169.     }
  170.     while (*s!=15) {
  171.       switch (*s++) {
  172.         case 1:
  173.         cx = ox + frx(&s); cy = oy + frx(&s);
  174.         g_move(cx,cy);
  175.         break;
  176.         case 2:
  177.         cx = cx + frx(&s); cy = cy + frx(&s);
  178.         g_line(cx,cy);
  179.         break;
  180.         case 3:
  181.         cx = cx + frx(&s); cy = cy + frx(&s);
  182.         x1 = cx; y1 = cy;
  183.         cx = cx + frx(&s); cy = cy + frx(&s);
  184.         x2 = cx; y2 = cy;
  185.         cx = cx + frx(&s); cy = cy + frx(&s);
  186.         g_bezier(x1,y1,x2,y2,cx,cy);
  187.         break;
  188.         case 4:
  189.         g_closepath();
  190.         break;
  191.         case 5:
  192.         if (!old_path)     g_fill();
  193.         break;
  194.         case 6:
  195.         g_stroke();
  196.         break;
  197.         case 7:
  198.         g_gsave();
  199.         g_set_fill(COLOR_WHITE);
  200.         g_fill();
  201.         g_grestore();
  202.         break;
  203.         case 8:
  204.         g_set_line_width(frx(&s));
  205.         break;
  206.         case 0:         /* no such char in this font */
  207.         goto abort;
  208.         default:
  209.         gprint("Error in mychar pcode %d \n",*s++);
  210.         goto abort;
  211.       }
  212.  
  213.     }
  214. abort:    if (!old_path) g_set_path(old_path);
  215.     g_set_line_join(old_join);
  216.     g_set_line_width(old_lwidth);
  217.     g_set_color(old_color);
  218.     return s-savelen;
  219. }
  220. double frx(char **s)
  221. {
  222.     static union {char a[2];short b;} both;
  223.     static int i;
  224.  
  225.     if (g.fontsz==0) {
  226.         gprint("Font size is zero ***\n");
  227.         g.fontsz = 1;
  228.     }
  229.     i = *(*s)++;
  230.     if (i==127) {
  231.         both.a[0] = (*(*s)++);
  232.         both.a[1] = (*(*s)++);
  233.         if (1==2) printf("bothb %d \n",both.b);
  234.         return (g.fontsz*both.b)/1000.0;
  235.     } else {
  236.             if (i>127) i = -(256-i);
  237.         if (1==2) printf("ii %d \n",i);
  238.         return (g.fontsz*i)/1000.0;
  239.     }
  240. }
  241. frxi(char **s)
  242. {
  243.     static union {char a[2];short b;} both;
  244.     static int i;
  245.  
  246.     i = *(*s)++;
  247.     if (i==127) {
  248.         both.a[0] = (*(*s)++);
  249.         both.a[1] = (*(*s)++);
  250.         if (1==2) printf("both %d \n",both.b);
  251.         return (both.b);
  252.     } else {
  253.             if (i>127) i = -(256-i);
  254.         if (1==2) printf("i %d \n",i);
  255.         return i;
  256.     }
  257. }
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.